package SmartHome.dao.impl;

import SmartHome.dao.DeviceDao;
import SmartHome.entity.DeviceInfo;
import SmartHome.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DeviceDaoImpl implements DeviceDao {
    public int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            ps = conn.prepareStatement(sql);
            // 遍历参数
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            // 4.执行语句
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, ps, null);
        }
        return 0;
    }

    @Override
    public void add(DeviceInfo deviceInfo) {
        String sql = "insert into DeviceInfo(deviceId,deviceName,deviceType,description,manufacturer,Exist) values (?,?,?,?,?,?)";
        this.executeUpdate(sql, deviceInfo.getDeviceId(), deviceInfo.getDeviceName(), deviceInfo.getDeviceType(), deviceInfo.getDescriptionn(), deviceInfo.getManufacturer(), deviceInfo.isExist());
    }

    @Override
    public void delete(int deviceId) {
        String sql = "update DeviceInfo set Exist=0 where deviceId = ?";
        this.executeUpdate(sql, deviceId);
    }

    @Override
    public DeviceInfo query(int deviceId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 1.加载驱动
            // 2.连接数据库
            conn = JdbcUtil.getConn();
            // 3.创建语句
            String sql = "select * from DeviceInfo where deviceId = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, deviceId);
            // 4.执行语句
            rs = ps.executeQuery();
            if (rs.next()) {
                DeviceInfo deviceInfo = new DeviceInfo(rs.getInt("deviceId"),
                        rs.getString("deviceName"), rs.getString("deviceType"),
                        rs.getString("description"), rs.getString("manufacturer"),
                        rs.getInt("deviceTypeId"), rs.getInt("familyID"),
                        rs.getBoolean("Exist"));
                return deviceInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 5.释放资源
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    @Override
    public void update(int deviceId, DeviceInfo deviceInfo) {
        String sql = "update deviceInfo set deviceId=?, deviceType=?, deviceName=?, description=?, manufacturer=?, Exist=? where deviceId =? ";
        this.executeUpdate(sql, deviceInfo.getDeviceId(), deviceInfo.getDeviceType(), deviceInfo.getDeviceName(),
                deviceInfo.getDescriptionn(), deviceInfo.getManufacturer(), deviceInfo.isExist(), deviceId);
    }

    public List<DeviceInfo> getAll() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            String sql = "select * from deviceInfo where Exist=1";
            System.out.println(sql);
            rs = st.executeQuery(sql);
            List<DeviceInfo> list = new ArrayList<DeviceInfo>();

            while (rs.next()) {
                DeviceInfo deviceInfo = new DeviceInfo(rs.getInt("deviceId"),
                        rs.getString("deviceName"), rs.getString("deviceType"),
                        rs.getString("description"), rs.getString("manufacturer"),
                        rs.getInt("deviceTypeId"), rs.getInt("familyID"),
                        rs.getBoolean("Exist"));
                list.add(deviceInfo);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, st, rs);
        }
        return null;
    }

}
